package com.bk.game.cat;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Disposable;
import com.bk.game.global.BkGlobal;
import com.bk.game.screen.GameScreen;

public class CatBtn extends Actor implements Disposable {

	private String name;

	private Sprite sprite;
	private float scaleX, scaleY;

	private float cx, cy;

	public CatBtn(String name, TextureRegion region, float left, float top) {
		this.name = name;
		sprite = new Sprite(region);
		scaleX = 120 * BkGlobal.factorx;
		scaleY = 120 * BkGlobal.factory;
		sprite.setSize(scaleX, scaleY);
		this.cx = left * BkGlobal.factorx;
		this.cy = top * BkGlobal.factory;
		sprite.setColor(0, 0, 0, 0);
		sprite.setPosition(this.cx, this.cy);
	}

	@Override
	public void dispose() {

	}

	@Override
	public void draw(SpriteBatch spriteBatch, float arg1) {
		sprite.draw(spriteBatch);
	}

	@Override
	public Actor hit(float arg0, float arg1) {
		if (arg0 > cx && arg1 > cy && arg0 < cx + sprite.getWidth()
				&& arg1 < cy + sprite.getHeight()) {
			return this;
		}
		return null;
	}

	@Override
	public boolean touchDown(float x, float y, int pointer) {
		sprite.setColor(0.2f, 0.2f, 0.2f, 0.8f);
		return true;
	}

	@Override
	public void touchDragged(float x, float y, int pointer) {
		super.touchDragged(x, y, pointer);
	}

	@Override
	public boolean touchMoved(float x, float y) {
		return super.touchMoved(x, y);
	}

	@Override
	public void touchUp(float x, float y, int pointer) {
		if (!BkGlobal.ISFINISHED) {
			if (BkGlobal.actors.size() > 0) {
				CatActor actor = BkGlobal.actors.get(0);
				if (actor.getName().equals(name)) {
					BkGlobal.getInstance().getRandomSound(actor.cat.index)
							.play();
					GameScreen.singlePressTime += 1;
					BkGlobal.score += 1;
					BkGlobal.flyActors.add(BkGlobal.actors.remove(0));
					actor.bang();
					actor.clearActions();
					BkGlobal.getInstance().flayAction(actor);
				} else {
					BkGlobal.buzzerSound.play();
					BkGlobal.ISFINISHED = true;
					BkGlobal.NORMAL_TIME = 6f;
					for (CatActor cat : BkGlobal.actors) {
						cat.clearActions();
					}
					for (CatActor cat : BkGlobal.flyActors) {
						cat.clearActions();
					}
				}
			}
		}
		sprite.setColor(0, 0, 0, 0);
	}

	public void reset() {
		sprite.setColor(0, 0, 0, 0);
	}
}
